package com.tancire.member.service.impl;

import java.util.List;
import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.utils.DateUtils;
import com.tancire.member.domain.Member;
import com.tancire.member.service.IMemberService;
import org.springframework.stereotype.Service;
import com.tancire.member.mapper.MemberTrainChartMapper;
import com.tancire.member.domain.MemberTrainChart;
import com.tancire.member.service.IMemberTrainChartService;

/**
 * 会员排行榜Service业务层处理
 *
 * @author tancire
 * @date 2024-10-23
 */
@Service
public class MemberTrainChartServiceImpl implements IMemberTrainChartService {

    @Resource
    private MemberTrainChartMapper memberTrainChartMapper;

    @Resource
    private IMemberService memberService;

    /**
     * 查询会员排行榜
     *
     * @param id 会员排行榜主键
     * @return 会员排行榜
     */
    @Override
    public MemberTrainChart selectMemberTrainChartById(Long id) {
        return memberTrainChartMapper.selectMemberTrainChartById(id);
    }

    /**
     * 查询会员排行榜列表
     *
     * @param memberTrainChart 会员排行榜
     * @return 会员排行榜
     */
    @Override
    public List<MemberTrainChart> selectMemberTrainChartList(MemberTrainChart memberTrainChart) {
        return memberTrainChartMapper.selectMemberTrainChartList(memberTrainChart);
    }

    /**
     * 新增会员排行榜
     *
     * @param memberTrainChart 会员排行榜
     * @return 结果
     */
    @Override
    public int insertMemberTrainChart(MemberTrainChart memberTrainChart) {
        memberTrainChart.setCreateTime(DateUtils.getNowDate());
        return memberTrainChartMapper.insertMemberTrainChart(memberTrainChart);
    }

    /**
     * 修改会员排行榜
     *
     * @param memberTrainChart 会员排行榜
     * @return 结果
     */
    @Override
    public int updateMemberTrainChart(MemberTrainChart memberTrainChart) {
        memberTrainChart.setUpdateTime(DateUtils.getNowDate());
        return memberTrainChartMapper.updateMemberTrainChart(memberTrainChart);
    }

    /**
     * 批量删除会员排行榜
     *
     * @param ids 需要删除的会员排行榜主键
     * @return 结果
     */
    @Override
    public int deleteMemberTrainChartByIds(Long[] ids) {
        return memberTrainChartMapper.deleteMemberTrainChartByIds(ids);
    }

    /**
     * 删除会员排行榜信息
     *
     * @param id 会员排行榜主键
     * @return 结果
     */
    @Override
    public int deleteMemberTrainChartById(Long id) {
        return memberTrainChartMapper.deleteMemberTrainChartById(id);
    }

    /**
     * 查询排行榜前100名
     *
     * @return
     */
    @Override
    public List<MemberTrainChart> selectMemberTrainChartTop() {
        return memberTrainChartMapper.selectMemberTrainChartList(new MemberTrainChart());
    }

    /**
     * 刷新排行榜
     */
    @Override
    public void refreshChart() {
        List<Member> members = this.memberService.selectMemberTopLevelList();
        int loopTime = members.size();
        if (loopTime > 20) {
            loopTime = 20;
        }
        List<MemberTrainChart> memberTrainChartList = this.selectMemberTrainChartTop();
        if (ObjectUtil.isNotEmpty(memberTrainChartList)) {
            for (int i = 0; i < loopTime; i++) {
                MemberTrainChart item = new MemberTrainChart();
                if (i < memberTrainChartList.size()) {
                    item = memberTrainChartList.get(i);
                }
                Member member = members.get(i);
                item.setMemberId(member.getMemberId());
                item.setMemberName(member.getNickName());
                item.setChartLevel(member.getMemberLevel());
                item.setMemberAvatar(member.getAvatar());
                // 判断是更新还是新增
                if (null == item.getId()) {
                    this.memberTrainChartMapper.insertMemberTrainChart(item);
                } else {
                    this.memberTrainChartMapper.updateMemberTrainChart(item);
                }
            }
        }
    }
}
